package net.chrone.quickpayapi.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;

import net.chrone.quickpayapi.model.Order;
import net.chrone.quickpayapi.model.PlatOrgInf;
import net.chrone.quickpayapi.service.OrderService;
import net.chrone.quickpayapi.service.PlatOrgInfService;
import net.chrone.quickpayapi.util.Constant;
import net.chrone.quickpayapi.util.SignatureUtil;

@Controller
@RequestMapping("/v1.0/order")
public class OrderQueryController {

	private  final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private PlatOrgInfService platOrgInfService;
	@Autowired
	private OrderService orderService;
	
	/**
	 * 查询订单结果
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,consumes="application/json;charset=UTF-8",value="/query")
	public void query(@RequestBody Order order,HttpServletResponse response){
		logger.debug("order:"+order);
		Map<String,String> respMap = new HashMap<String, String>();
		if(StringUtils.isEmpty(order.getOrgId())){
			logger.error("机构号不能为空");
			respMap.put("respCode","401");
			response(respMap,response);
			return;
		}
		PlatOrgInf platOrgInf = platOrgInfService.findById(order.getOrgId());
		if(platOrgInf==null){
			logger.error("找不到机构信息,orgId:"+order.getOrgId());
			respMap.put("respCode","402");
			response(respMap,response);
			return;
		}
		boolean flag = SignatureUtil.verify(order.getSignature(), order,platOrgInf.getPublicKey());
		if(!flag){
			logger.error("验签失败");
			respMap.put("respCode","403");
			response(respMap,response);
			return;
		}
		Order o = orderService.find(order.getOrgId(), order.getOrgOrderNo());
		if(o==null){
			respMap.put("respCode", "416");//订单不存在
			response(respMap,response);
			return;
		}else{
			respMap.put("account",o.getAccount());
			respMap.put("amount",o.getAmount()+"");
			respMap.put("body",o.getBody());
			respMap.put("description",o.getDescription());
			respMap.put("extra",o.getExtra());
			respMap.put("fee",o.getFee()+"");
			respMap.put("orderDt",o.getOrderDt());
			respMap.put("orderNo",o.getOrderNo());
			respMap.put("orgId",o.getOrgId());
			respMap.put("orgOrderNo",o.getOrgOrderNo());
			respMap.put("paySt",o.getPaySt()+"");
			respMap.put("respCode","200");
			respMap.put("subject",o.getSubject());
			response(respMap,response);
			return;
		}
	}
	
	private void response(Map<String, String> respMap,HttpServletResponse response) {
		respMap.put("respMsg", Constant.getErrorCodeMap().get(respMap.get("respCode")));
		respMap.put("signature", SignatureUtil.signResp(respMap));
		String json = JSON.toJSONString(respMap);
		try {
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
